package com.ssm.forum.rpc.service.impl.content;

import com.github.pagehelper.PageInfo;
import com.ssm.forum.dao.mapper.assortment.AssortmentDao;
import com.ssm.forum.dao.mapper.content.ContentDao;
import com.ssm.forum.dao.mapper.reply.ReplyDao;
import com.ssm.forum.dao.mapper.residence.ResidenceDao;
import com.ssm.forum.dao.mapper.user.UserDao;
import com.ssm.forum.dao.model.content.Content;
import com.ssm.forum.rpc.api.content.ContentService;
import com.ssm.icc.dao.mapper.estate.EstateDao;
import com.ssm.upms.dao.mapper.ssm.AdminDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
* 帖子内容管理 Service
* @author GaoXiang
* @version 1.0
*/
@Service
public class ContentServiceImpl implements ContentService {

    @Resource
    private ContentDao contentDao;

    @Resource
    private AssortmentDao assortmentDao;

    @Resource
    private ReplyDao replyDao;

    @Resource
    private UserDao userDao;


    @Resource
    private ResidenceDao residenceDao;

    @Resource
    private AdminDao adminDao;

   /* @Resource
    private EstateDao estateDao;
*/


    /**
    * 保存数据
    * @param content 实体对象
    * @return 实体id
    * @throws Exception 数据保存异常
    */
    public Integer insert(Content content) throws Exception {
        return contentDao.insert(content);
    }

    /**
    * 根据 id 修改
    * @param content 带id的实体对象
    * @return 受影响的行数
    * @throws Exception 数据修改异常
    */
    public Integer updateById(Content content) throws Exception {
        return contentDao.updateById(content);
    }

    /**
    * 根据 id 删除
    * @param id 数据id
    * @return 受影响的行数
    * @throws Exception 数据删除异常
    */
    public Integer deleteById(String id) throws Exception {
        replyDao.deleteByContentId(id);
        return contentDao.deleteById(id);
    }

    /**
    * 根据 id 查找
    * @param id 实体id
    * @return 实体
    */
    public Content selectById(String id) {
        Content content=contentDao.selectById(id);
        content.setAssortment(assortmentDao.selectById(content.getAssortId()));
        content.setUser(userDao.selectById(content.getUserId()));
        return content;
    }

    /**
    * 根据 id 批量删除
    * @param ids 要删除的id
    * @return 影响的行数
    * @throws Exception 数据删除异常
    */
    public Integer deleteByIds(List<String> ids) throws Exception {
        replyDao.deleteByContentIds(ids);
        return contentDao.deleteByIds(ids);
    }

    /**
    * 查询列表
    * @param map 参数
    * @return 列表
    */
    public PageInfo<Content> list(Map map) {
        return contentDao.list(map);
    }

    /**
    * 查询列表 带分页
    * @param map 参数
    * @param page 页码
    * @param size 每页大小
    * @return 列表
    */
    public PageInfo<Content> list(Map map, int page, int size){
        PageInfo<Content> contentPageInfo=contentDao.list(map,page,size);
        for (Content content:contentPageInfo.getList()){
            content.setAssortment(assortmentDao.selectById(content.getAssortId()));
             if(content.getAuthorType().equals("1")){
                 content.setAdmin(adminDao.selectById(content.getUserId()));
            }/*else if (content.getAuthorType().equals("2")){
                content.setEstate( estateDao.selectById(content.getUserId()));
            }*/else if(content.getAuthorType().equals("3")){
                 content.setRe(residenceDao.selectById(content.getUserId()));
             }else if(content.getAuthorType().equals("4")){
                content.setUser(userDao.selectById(content.getUserId()));
            }
            content.setResidence(residenceDao.selectById(content.getResidenceId()));
        }
        return  contentPageInfo;
    }

    /**
     * 根据小区ID查询帖子
     * 2017-6-27 14:30:09 韩榕榕
     * @param residenceId
     * @return
     */
    public List<Content> selectByResidenceId(String residenceId){
        return  contentDao.selectByResidenceId(residenceId);
    }

    /**
     * 根据小区的ID删除帖子
     * 2017-6-27 14:31:01 韩榕榕
     * @param residenceId
     * @return
     * @throws Exception
     */
    public Integer deleteByResidenceId(String residenceId) throws Exception {
        return  contentDao.deleteByResidenceId(residenceId);
    }
}
